package com.aierkeji.call.cms.dao.jpa;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.aierkeji.call.cms.dao.BaseJPADao;
import com.aierkeji.call.cms.dao.jpa.custom.AkjCallUserDaoCustom;
import com.aierkeji.call.cms.entity.AkjCallUser;
import com.aierkeji.call.cms.entity.vo.SearchOptionsVo;

public interface AkjCallUserDao extends BaseJPADao<AkjCallUser, Long>, AkjCallUserDaoCustom {

	int countByAccountAndIsDelete(@Param("account") String account, @Param("isDelete") Boolean isDelete);

	AkjCallUser findByAccountAndIsDelete(@Param("account") String account, @Param("isDelete") Boolean isDelete);

	@Query("select new com.aierkeji.call.cms.entity.vo.SearchOptionsVo(name,id) from AkjCallUser where (leaderId=:leaderId or (:leaderId is null and leaderId is not null)) and isDelete=0 order by name desc")
	List<SearchOptionsVo> findCanAssigneUser(Long leaderId);

	@Query("select new com.aierkeji.call.cms.entity.vo.SearchOptionsVo(t.name,t.id) from AkjCallUser t join t.roles a where a.code = :roleId  and t.isDelete=0 order by t.id desc")
	List<SearchOptionsVo> findUserByRoleId(String roleId);

	
	@Query(" select role.name  from AkjCallUser t join t.roles role  where t.id = :id order by role.id desc")
	public List<String> findRoleName(@Param("id") Long userId);

	@Modifying
	@Query("update AkjCallUser set pwd = :pwd ,updatedBy.id = :id,updatedAt = :updatedAt where id = :id and leaderId=:leaderId")
	int resetPassword(@Param("id") Long id, @Param("leaderId") long leaderId, @Param("pwd") String password,
			@Param("updatedAt") Timestamp currentTime);
	
	@Modifying
	@Query("update AkjCallUser set isDelete = :isDelete ,updatedBy.id = :updatedBy,updatedAt = :updatedAt,account=CONCAT(account,:remark) where leaderId = :leaderId and isDelete = 0")
	int deleteSubUser( @Param("isDelete") boolean isDelete, @Param("leaderId") Long leaderId, @Param("updatedBy") Long updatedBy,
			@Param("updatedAt") Date currentTime,@Param("remark") String remark);
	
}
